//
//  AppDelegate.h
//  进度条
//
//  Created by 李尹卓 on 16/11/18.
//  Copyright © 2016年 lyz. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;
- (IBAction)onStartDownload:(id)sender {
    NSURL *url = [NSURL URLWithString:@"http://image.tianjimedia.com/uploadImages/2013/057/4JI2LJ8JAZMC.jpg"];
    NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:[NSOperationQueue mainQueue]];
    NSURLSessionDataTask *task = [session dataTaskWithURL:url];
    [task resume];
    self.imgProgress.progress = 0;
    self.imgView.image = nil;
}

- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error
{
    self.imgView.image = [UIImage imageWithData:imgData];
}

- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data
{
    [self.imgProgress setProgress:(float)dataTask.countOfBytesReceived/(float)dataTask.countOfBytesExpectedToReceive animated:YES];
    if (!imgData) {
        imgData = [NSMutableData new];
    }
    [imgData appendData:data];
}

@end

